import { createStore } from 'vuex'

export default createStore({
  state: {
    // 用户信息
    userId: localStorage.getItem('userId') || '',
    sessionId: localStorage.getItem('sessionId') || '',
    userPhone: localStorage.getItem('userPhone') || '',
    
    // 客服信息
    adminId: localStorage.getItem('adminId') || '',
    adminName: localStorage.getItem('adminName') || ''
  },
  mutations: {
    // 设置用户信息
    SET_USER_INFO(state, { userId, sessionId, phone }) {
      state.userId = userId
      state.sessionId = sessionId
      state.userPhone = phone || ''
      
      localStorage.setItem('userId', userId)
      localStorage.setItem('sessionId', sessionId)
      if (phone) {
        localStorage.setItem('userPhone', phone)
      }
    },
    
    // 设置客服信息
    SET_ADMIN_INFO(state, { adminId, adminName }) {
      state.adminId = adminId
      state.adminName = adminName
      
      localStorage.setItem('adminId', adminId)
      localStorage.setItem('adminName', adminName)
    },
    
    // 清除用户信息
    CLEAR_USER_INFO(state) {
      state.userId = ''
      state.sessionId = ''
      state.userPhone = ''
      
      localStorage.removeItem('userId')
      localStorage.removeItem('sessionId')
      localStorage.removeItem('userPhone')
    },
    
    // 清除客服信息
    CLEAR_ADMIN_INFO(state) {
      state.adminId = ''
      state.adminName = ''
      
      localStorage.removeItem('adminId')
      localStorage.removeItem('adminName')
    }
  },
  actions: {
    // 设置用户信息
    setUserInfo({ commit }, userInfo) {
      commit('SET_USER_INFO', userInfo)
    },
    
    // 设置客服信息
    setAdminInfo({ commit }, adminInfo) {
      commit('SET_ADMIN_INFO', adminInfo)
    },
    
    // 登出（清除所有信息）
    logout({ commit }) {
      commit('CLEAR_USER_INFO')
      commit('CLEAR_ADMIN_INFO')
    }
  },
  modules: {
  }
})
